package me.hetong.caigou.ui.chessboard;

import javafx.application.Platform;
import javafx.geometry.VPos;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.text.TextAlignment;
import me.hetong.caigou.Global;

/**
 * 棋盘CANVAS
 *
 * @author hetong
 */
public class ChessboardWarOnlyBoard {

    public static int GRID_SIZE = Global.config.getGridSize();// 每个方格的宽高
    public static int GRID_SIZE_HALF = (int) (GRID_SIZE * 0.5);// 每个方格的宽高
    public static int CHESS_SIZE = GRID_SIZE;// 每个方格的宽高
    public static Canvas CANVAS = new Canvas(GRID_SIZE * 9 + GRID_SIZE_HALF, GRID_SIZE * 10 + GRID_SIZE_HALF);
    private static final GraphicsContext GC = CANVAS.getGraphicsContext2D();
    private static final int CROSS_PY = 2;// 十字交叉点偏移量
    private static final int crossLength = 8;// 十字交叉点线长度
    private static final int lineWidth = 1;// 默认线宽
    private static final int outLineWidth = 2;// 棋盘外边框线宽
    private static final int outLinePy = 4;// 棋盘外边框偏移量
    private static final String backgroundColor = Global.config.getBackgroundColorBoardActual();
    private static final Image chuheImage = new Image("image/chessboard/ch.png");// 楚河图片
    private static final Image hanjieImage = new Image("image/chessboard/hj.png");// 汉界图片

    static {
        GC.setTextAlign(TextAlignment.CENTER);
        GC.setTextBaseline(VPos.CENTER);
        draw();
    }

    public static void reSize(int gridSize) {
        GRID_SIZE = gridSize;
        CANVAS.setWidth(GRID_SIZE * 9 + GRID_SIZE_HALF);
        CANVAS.setHeight(GRID_SIZE * 10 + GRID_SIZE_HALF);
        GRID_SIZE_HALF = (int) (GRID_SIZE * 0.5);// 每个方格的宽高
        CHESS_SIZE = GRID_SIZE;// 每个方格的宽高
        draw();
    }

    public static void draw() {
        Platform.runLater(() -> {
            GC.clearRect(0, 0, CANVAS.getWidth(), CANVAS.getHeight());
            GC.setFill(Color.valueOf(backgroundColor));
            GC.fillRect(0, 0, CANVAS.getWidth(), CANVAS.getHeight());

            // 画横线
            for (int i = 0; i <= 9; i++) {
                drawLine(0, i, 8, i);
            }

            // 画竖线
            drawLine(0, 0, 0, 9);
            for (int i = 1; i <= 7; i++) {
                drawLine(i, 0, i, 4);
                drawLine(i, 5, i, 9);
            }
            drawLine(8, 0, 8, 9);

            // 画边框粗线
            drawBoardLine();

            // 画斜线叉
            drawLine(3, 0, 5, 2);
            drawLine(5, 0, 3, 2);
            drawLine(3, 7, 5, 9);
            drawLine(5, 7, 3, 9);

            // 画十字
            drawCross4(1, 2);
            drawCross4(7, 2);
            drawCross4(2, 3);
            drawCross4(4, 3);
            drawCross4(6, 3);
            drawCross4(2, 6);
            drawCross4(4, 6);
            drawCross4(6, 6);
            drawCross4(1, 7);
            drawCross4(7, 7);
            drawCross2L(0, 3);
            drawCross2L(0, 6);
            drawCross2R(8, 3);
            drawCross2R(8, 6);

            for (int i = 0; i < 9; i++) {
                drawTextTop(i, String.valueOf(i + 1));
            }

            drawTextBottom(0, "九");
            drawTextBottom(1, "八");
            drawTextBottom(2, "七");
            drawTextBottom(3, "六");
            drawTextBottom(4, "五");
            drawTextBottom(5, "四");
            drawTextBottom(6, "三");
            drawTextBottom(7, "二");
            drawTextBottom(8, "一");

            int py = (int) (GRID_SIZE_HALF * 9.5);
            double px = GRID_SIZE * 1.5;
            GC.drawImage(chuheImage, px, py, GRID_SIZE, GRID_SIZE);
            GC.drawImage(hanjieImage, (int) (CANVAS.getWidth() - GRID_SIZE - px), py, GRID_SIZE, GRID_SIZE);
        });
    }

    /**
     * 画直线
     *
     * @param x1
     * @param y1
     * @param x2
     * @param y2
     */
    private static void drawLine(int x1, int y1, int x2, int y2) {
        GC.strokeLine(x1 * GRID_SIZE + GRID_SIZE_HALF * 1.5, y1 * GRID_SIZE + GRID_SIZE_HALF * 1.5, x2 * GRID_SIZE + GRID_SIZE_HALF * 1.5, y2 * GRID_SIZE + GRID_SIZE_HALF * 1.5);
    }

    /**
     * 画棋盘十字
     *
     * @param x
     * @param y
     */
    private static void drawCross4(int x, int y) {
        drawCross2L(x, y);
        drawCross2R(x, y);
    }

    /**
     * 画棋盘左半边十字
     *
     * @param x
     * @param y
     */
    private static void drawCross2L(int x, int y) {
        // 中心点
        x = x * GRID_SIZE + GRID_SIZE_HALF + GRID_SIZE_HALF / 2;
        y = y * GRID_SIZE + GRID_SIZE_HALF + GRID_SIZE_HALF / 2;

        // 右上
        GC.strokeLine(x + CROSS_PY, y - CROSS_PY, x + CROSS_PY + crossLength, y - CROSS_PY);
        GC.strokeLine(x + CROSS_PY, y - CROSS_PY, x + CROSS_PY, y - CROSS_PY - crossLength);
        // 右下
        GC.strokeLine(x + CROSS_PY, y + CROSS_PY, x + CROSS_PY + crossLength, y + CROSS_PY);
        GC.strokeLine(x + CROSS_PY, y + CROSS_PY, x + CROSS_PY, y + CROSS_PY + crossLength);
    }

    /**
     * 画棋盘右半边十字
     *
     * @param x
     * @param y
     */
    private static void drawCross2R(int x, int y) {
        // 中心点
        x = x * GRID_SIZE + GRID_SIZE_HALF + GRID_SIZE_HALF / 2;
        y = y * GRID_SIZE + GRID_SIZE_HALF + GRID_SIZE_HALF / 2;
        // 左上
        GC.strokeLine(x - CROSS_PY, y - CROSS_PY, x - CROSS_PY - crossLength, y - CROSS_PY);
        GC.strokeLine(x - CROSS_PY, y - CROSS_PY, x - CROSS_PY, y - CROSS_PY - crossLength);
        // 左下
        GC.strokeLine(x - CROSS_PY, y + CROSS_PY, x - CROSS_PY - crossLength, y + CROSS_PY);
        GC.strokeLine(x - CROSS_PY, y + CROSS_PY, x - CROSS_PY, y + CROSS_PY + crossLength);
    }

    private static void drawTextTop(int x, String text) {
        GC.setFill(Paint.valueOf("#000"));
        GC.fillText(text, x * GRID_SIZE + GRID_SIZE_HALF * 1.5, GRID_SIZE_HALF * 1.5 - 15);
    }

    private static void drawTextBottom(int x, String text) {
        GC.fillText(text, x * GRID_SIZE + GRID_SIZE_HALF * 1.5, 9 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + 15);
    }

    private static void drawBoardLine() {
        GC.setLineWidth(outLineWidth);
        GC.strokeLine(GRID_SIZE_HALF * 1.5 - outLinePy, GRID_SIZE_HALF * 1.5 - outLinePy, 8 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy, 0 * GRID_SIZE + GRID_SIZE_HALF * 1.5 - outLinePy);
        GC.strokeLine(GRID_SIZE_HALF * 1.5 - outLinePy, GRID_SIZE_HALF * 1.5 - outLinePy, +GRID_SIZE_HALF * 1.5 - outLinePy, 9 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy);
        GC.strokeLine(GRID_SIZE_HALF * 1.5 - outLinePy, 9 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy, 8 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy, 9 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy);
        GC.strokeLine(8 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy, 0 * GRID_SIZE + GRID_SIZE_HALF * 1.5 - outLinePy, 8 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy, 9 * GRID_SIZE + GRID_SIZE_HALF * 1.5 + outLinePy);
        GC.setLineWidth(lineWidth);
    }

    private void drawArrow(double fromX, double fromY, double toX, double toY, String color) {

        int theta = 30;
        int headlen = 10;
        int width = 3;

        // 计算各角度和对应的P2,P3坐标
        double angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI,
                angle1 = (angle + theta) * Math.PI / 180,
                angle2 = (angle - theta) * Math.PI / 180,
                topX = headlen * Math.cos(angle1),
                topY = headlen * Math.sin(angle1),
                botX = headlen * Math.cos(angle2),
                botY = headlen * Math.sin(angle2);

        GC.save();
        GC.beginPath();

        double arrowX = fromX - topX;
        double arrowY = fromY - topY;

        GC.moveTo(arrowX, arrowY);
        GC.moveTo(fromX, fromY);
        GC.lineTo(toX, toY);
        arrowX = toX + topX;
        arrowY = toY + topY;
        GC.moveTo(arrowX, arrowY);
        GC.lineTo(toX, toY);
        arrowX = toX + botX;
        arrowY = toY + botY;
        GC.lineTo(arrowX, arrowY);
        GC.setStroke(Paint.valueOf(color));
        GC.setLineWidth(width);
        GC.stroke();
        GC.restore();
    }

//    private static void drawTextTop(int x, String text) {
//        GC.setFill(Color.valueOf("#3d3d3d"));
//        GC.fillText(text, x * GRID_SIZE + GRID_SIZE_HALF * 1.5, 0 * GRID_SIZE + GRID_SIZE_HALF * 1.5 - 15);
//    }

//    private void drawTextBottom(int x, String text) {
//        gc.fillText(text, x * gridSize + gridSize / 2 * 1.5, 9 * gridSize + gridSize / 2 * 1.5 + 15);
//    }
}